package org.sacinfo.realm;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.sacinfo.entity.User;
import org.sacinfo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomRealm extends AuthorizingRealm {

/*	@Resource
	private UserService userService;*/
	
	
	// 用于认证
		//没有连接数据库的方法
		@Override
		protected AuthenticationInfo doGetAuthenticationInfo(
				AuthenticationToken token) throws AuthenticationException {
			//User user = userService.getUserByUsername("sac");
			User user=new User();
			user.setUsername("xxx");
			user.setPassword("111111");
			
			// token是用户输入的用户名和密码 
			// 第一步从token中取出用户名
			String userCode = (String) token.getPrincipal();
			String salt = "qwerty";
			// 第二步：根据用户输入的userCode从数据库查询
			// ....

			// 如果查询不到返回null
			// 模拟从数据库查询到密码
			String password = "111111";
			password="f3694f162729b7d0254c6e40260bf15c";//加盐散列后

			// 如果查询到返回认证信息AuthenticationInfo
			
			//activeUser就是用户身份信息
			//..
			
			//根据用户id取出菜单
			//通过service取出菜单 
			
			
			//将activeUser设置simpleAuthenticationInfo
			SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
					userCode, password,ByteSource.Util.bytes(salt) ,this.getName());

			return simpleAuthenticationInfo;
		}
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}


}
